<?php
//
namespace app\home\controller;

use app\common\model\ShopGoodsDataModel;
use app\common\model\ShopGoodsExtendModel;
use app\common\model\ShopGoodsModel;
use app\common\model\ShopGoodsSkuModel;
use app\common\model\ShopPromotionGroupbuyCartModel;
use think\Request;

class PromotionController extends BaseController
{

    private $goodsCategory;
    private $goodsModel;
    private $goodsSkuModel;
    private $goodsDataModel;
    private $goodsExtendModel;
    private $promotionType;
    private $promotionTpl;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->goodsCategory = goods_cache_cat($this->siteId);
        $this->goodsModel       = new ShopGoodsModel();
        $this->goodsSkuModel    = new ShopGoodsSkuModel();
        $this->goodsDataModel   = new ShopGoodsDataModel();
        $this->goodsExtendModel = new ShopGoodsExtendModel();
        $this->promotionType = $this->request->param('promotion_type/d');
        $this->promotionTpl = [1=>'presell',2=>'seckill',3=>'point',4=>'groupbuy',5=>'auction'];
        $this->assign('promotionType',$this->promotionType);
    }

    /*
     * 活动频道页
     */
    public function channel()
    {
        return $this->fetch($this->promotionTpl[$this->promotionType].'_channel');
    }

    /*
     * 活动列表页
     */
    public function itemlist()
    {
        $cid        = $this->request->param('cid/d',0);
        $extendQuery = [];
        $extendSearch['cid'] = $cid;
        $extendSearch['promotion_type'] = $this->promotionType;
        $extendQuery['promotion_type'] = $this->promotionType;
        $extendQuery['promotion_status'] = $this->request->param('promotion_status',1);
        $extendSearch['promotion_status'] = $this->request->param('promotion_status',1);
        $itemList   = goods_page_list($this->siteId, $cid, '12', $extendQuery, $extendSearch);
        if ($itemList) {
            $goods=$itemList->toArray();
            $goodsIds = array_column($goods['data'], 'goods_id');
            $arr  = ['1' => 'ShopPromotionPresellModel', '2' => 'ShopPromotionLimitModel', '3' => 'ShopPromotionPointModel', '4' => 'ShopPromotionGroupbuyModel', '5' => 'ShopPromotionAuctionModel'];
            $promotionList = model($arr[$this->promotionType])->where('goods_id','in', $goodsIds)->column('*', 'goods_id');
            foreach ($itemList as $k => $v) {
                $itemList[$k]['promotion_info'] = $promotionList[$v['goods_id']];
            }
        }
        if($this->request->isAjax()){
            return $itemList;
        }
        $this->assign('itemList', $itemList);
        $this->assign('extendSearch', $extendSearch);
        return $this->fetch($this->promotionTpl[$this->promotionType].'_list');
    }

    /**
     * 活动内容页
     */
    public function item()
    {
        $skuId           = $this->request->param('sku_id/d');
        $skuInfo         = $this->goodsSkuModel->find($skuId)->toArray();
        $goodsInfo       = $this->goodsModel->getInfo($skuInfo['goods_id']);
        $goodsInfo['promotion_info']=[];
        if($goodsInfo['promotion_type']>0){
            $promotionInfo = $this->goodsModel->checkPromotionGoods($goodsInfo['goods_id'],$goodsInfo['promotion_type']);
            $goodsInfo['promotion_info'] = is_numeric($promotionInfo)?[]:$promotionInfo;
        }
        $goodsExtendList = [];
        if ($goodsInfo['extend_id'] > 0) {
            $goodsExtendList = $this->goodsExtendModel->where('goods_id', $skuInfo['goods_id'])->order('item_sort')->select()->toArray();
        }
        $goodsInfo['attr']        = goods_sku_name($goodsInfo);
        $goodsInfo['extend_list'] = $goodsExtendList;
        $goodsInfo                = array_merge($goodsInfo, $skuInfo);
        $goodsInfo                = format_goods_content($goodsInfo);
        if ($this->promotionType==4){
            $promotionCartModel=new ShopPromotionGroupbuyCartModel();
            $cartInfo=$promotionCartModel->where(['sku_id'=>$skuInfo['sku_id'],'member_uid'=>$this->getMemberUid()])->find();
            $cartList=$promotionCartModel->where(['sku_id'=>$skuInfo['sku_id'],'margin_status'=>1])->select();
            $jicainum=$promotionCartModel->where(['sku_id'=>$goodsInfo['sku_id'],'margin_status'=>1])->sum('num');
            if(empty($jicainum)){
                $jicainum=0;
            }
            if ($cartInfo){
                $this->assign('cartInfo',$cartInfo);
                if ($cartInfo['margin_status']==1){
                    $cartStatus=2;
                }else{
                    $cartStatus=1;
                }
            }else{
                $cartStatus=0;
            }
            $this->assign('jicainum',$jicainum);//已集采数量
            $this->assign('cartStatus',$cartStatus);
            $this->assign('cartStatus',$cartStatus);
            $this->assign('cartList',$cartList);
        }
        //dump($goodsInfo);
        $this->assign('keyword',$goodsInfo['keywords']);
        $this->assign('description',$goodsInfo['description']);
        $this->assign('goodsInfo', $goodsInfo);
        return $this->fetch($this->promotionTpl[$this->promotionType].'_content');
    }

    /**
     * 参加集采
     */
    public function promotionGroupBuyJoin()
    {
        $promotionModel = new ShopPromotionGroupbuyCartModel();
        $data=$this->request->param();
        $data['memberUid']=$this->getMemberUid();
        $data['siteId']=$this->siteId;
        $orderInfo=$promotionModel->subCart($data['sku_id'],$data['num'],$this->userInfo['uid']);
        if ($orderInfo){
            if ($orderInfo['margin_status']){
                $this->success('集采成功',get_current_url());
            }else{
                $this->success('请先支付',url('/home/Cart/payOrder',['order_sn'=>$orderInfo['order_sn'],
                    'moudle'=>'ShopPromotionGroupbuyCartModel',
                    'tpl'=>'groupbuy']));
            }
        }else{
            $this->error($promotionModel->getError());
        }
    }

    /**
     * 获取价格
     * @return array
     */
    public function getPrice()
    {
        $data=$this->request->param();
        if ($data['promotion_type']==4){
            $cartModel=new ShopPromotionGroupbuyCartModel();
            $num=$cartModel->where(['sku_id'=>$data['sku_id'],'margin_status'=>1])->sum('num')+$data['num'];
        }else{
            $num=$data['num'];
        }
        $skuInfo         = $this->goodsSkuModel->find($data['sku_id'])->toArray();
        $priceData=[];
        $priceData['price']=$this->goodsModel->calcPrice($skuInfo['goods_price'],$num);
        $priceData['money']=$priceData['price']*$data['num'];
        $priceData['price']=round($priceData['price'] ,2);
        $priceData['money']=round($priceData['money'] ,2);
        return $priceData;
    }

}
